﻿#include <QWidget>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QFormLayout>
#include <QLineEdit>
#include <QTextEdit>
#include <QPushButton>
#include <QLabel>
#include <QScrollArea>
#include "globaldata.h"

class UnitInfoEditor : public QWidget
{
    Q_OBJECT
public:
    explicit UnitInfoEditor(QWidget *parent = nullptr);
    void setUnit(Unit *unit);

public slots:
    void onApplyClicked();

private:
    struct ConstautEditGroup
    {
        QLineEdit *index;
        QLineEdit *name;
        QLineEdit *value;
        QLineEdit *unit;
        QLineEdit *desc;
    };

    void createWidgets();
    void setupLayout();
    void createUnitGroup();
    void createConstautsGroup(QWidget *content);
    void loadUnitData();
    void addConstautEditGroup(const Unit::Constaut &c);
    QLineEdit *createConstautEdit(const QString &text, QFormLayout *layout, const QString &label);

    // Unit组控件
    QGroupBox *unitGroup = nullptr;
    QLineEdit *idEdit = nullptr;
    QLineEdit *typeEdit = nullptr;
    QLineEdit *nameEdit = nullptr;
    QTextEdit *descEdit = nullptr;
    QLineEdit *codeTypeEdit = nullptr;
    QLineEdit *codeValueEdit = nullptr;

    // Constauts组控件
    QGroupBox *constantsGroup = nullptr;
    QWidget *constantsContainer = nullptr;
    QVBoxLayout *constantsLayout = nullptr;
    QList<ConstautEditGroup> constantEdits;
    QList<QWidget*> constantEditsWidgets;

    QPushButton* applyButton = nullptr;
    Unit *currentUnit = nullptr;
};
